package com.xcx.robot.mirai.factory.facade;//package com.xcx.robot.mirai.factory.facade;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.mirai.entity.JX3API;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.KeyUtils;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Author: 邢晨旭
 * @Date: 2021/5/26 16:18
 * @Description:
 */
@Service
@Slf4j
public class GoldFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.GOLD;


    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        String keyByWord = KeyUtils.getKeyByWord(content, 2);
        String url = JX3API.GOLD;
        StringBuilder result = new StringBuilder();
        if (StringUtils.isEmpty(keyByWord)) {
            url = String.format(url, "剑胆琴心");
            result.append("(●'剑胆琴心・金价'●)\n");
        } else {
            url = String.format(url, keyByWord);
            result.append("(●'").append(keyByWord).append("・金价'●)\n");
        }
        String jsonStr = HttpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        String code = jsonObject.getString("code");
        if ("200".equals(code)) {
            JSONObject gold = JSONObject.parseObject(jsonObject.getString("data"));
            result.append(" 万宝楼：").append(gold.get("wanbaolou")).append("\n")
                    .append("   5173：").append(gold.get("5173")).append("\n")
                    .append(" uu898：").append(gold.get("uu898")).append("\n")
                    .append(" dd373：").append(gold.get("dd373")).append("\n")
                    .append("   7881：").append(gold.get("7881"));
            receiver.sendMessage(result.toString());
        } else {
            receiver.sendMessage("没有查到" + keyByWord + "的金价");
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
